/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.NamingException;

/**
 * Configuration de GlassFish:
 *  Session JavaMail:
 *      Nom du JNDI: mail/esat
 *      hote mail: smtp.ent-etrs.net
 *      Utilisateur par défaut: loic.vaugeois@ent-etrs.net
 *      Adresse par défaut: loic.vaugeois@ent-etrs.net
 *      Activé
 *
 *      Avancé:
 *      Protocole de transport: SMTP
 *      Classe de protocole de transport: com.sun.mail.smtp.SMTPTransport
 *
 *      Autres propriétés:
 *      mail.smtp.port                     25
 *      mail.smtp.auth                     true
 *      mail.smtp.password                 ********
 *      mail.smtp.enabled                  true
 *
 *
 *
 * @author loic.vaugeois
 */

@Stateless
public class Mail implements MailLocal{
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 ********************************ATTRIBUTS
 */
    @Resource(name = "mail/esat")
    private Session mailSession;


/*
 ********************************CONSTRUCTEUR
 */
    /**
     * Constructeur nécessaire à JPA
     */
    public Mail() {
    }

/*
 *****************************METHODES
 */

 /**
  * Envoi de mail
  * @param email
  * @param subject
  * @param body
  * @throws NamingException
  * @throws MessagingException
  */
    @Override
    public void sendMail(String email, String subject, String body) throws NamingException, MessagingException {
        try{
        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject(subject);
        message.setRecipients(RecipientType.TO, InternetAddress.parse(email, false));
        message.setContent(body, "text/html; charset=utf-8");
        Transport.send(message);
        }catch (SendFailedException ex){
            Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Le message n'a pu être envoyé "+ex);
        }

    }

}








